/* Copyright 2012 The Go Authors.  All rights reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file.
 */
'use strict';

/* Services */

angular.module('tour.services', []).

	// Internationalization
	factory('I18n', function() {
		var tr = {
			"off": "off",
			"on": "on",
			"syntax": "Syntax-Highlighting",
			"lineno": "Line-Numbers",
			"reset": "Reset Slide",
			"format": "Format Source Code",
			"kill": "Kill Program",
			"run": "Run",
			"compile": "Compile and Run",
			"more": "Options",
			"toc": "Table of Contents",
			"prev": "Previous",
			"next": "Next",
			"waiting": "Waiting for remote server...",
			"errcomm": "Error communicating with remote server.",
		};
		return {
			L: function(key) {
				if (tr[key]) {
					return tr[key]
				}
				return "(no translation for "+key+")";
			}
		}
	}).

	// Navigation
	factory('Nav', ['$location', 'Module',  '$routeParams',
		function($location, Module, $routeParams) {

		var listeners = [];
		var	curPage = 0;
		var pages = [];
		var moduleId = undefined;
		var nav = undefined;

		var finish = function(success, nav) {
			success(nav);
			// goto to call all the listeners
			nav.goto(curPage);
		}

		return function(success) {
			if (moduleId == $routeParams.moduleId) {
				finish(success, nav);
				return
			}
			Module($routeParams.moduleId, function(mod) {
				moduleId = $routeParams.moduleId;
				curPage = parseInt($routeParams.pageNumber);
				nav = {
					// the current module.
					module: mod,

					// add a new listener to the page change event.
					onPageChange: function(cb) { listeners.push(cb); },

					// change the current page and notify all the listeners.
					goto: function(p) {
						if (p < 0 || p >= mod.Pages.length) {
							return;
						}
						curPage = p;

						$location.path(
							'/module/'+
							$routeParams.moduleId+'/'+
							curPage);

						for (var l in listeners) {
							listeners[l](mod.Pages[curPage]);
						}
					},

					// go to next page.
					next: function() { nav.goto(curPage+1); },

					// go to previous page.
					prev: function() { nav.goto(curPage-1); },

					// current page getter.
					current: function() { return curPage; },

					// module name.
					name: function() { return mod.Title; }
				};
				finish(success, nav);
			});
		}
	}]).

	// Module loading
	factory('Module', ['$http', function($http) {
		return function(moduleId, success) {
		 	$http.get('/module/'+moduleId, {cache: true}).
		  		error(function(data, status, headers, config) {
		  			console.log(data, status)
		  		}).
		  		success(function(data) {
		  			success(data);
		  		});
		}
	}]).

	// Running code
	factory('Run', function() {
		return function(code, output) {
	  		window.transport.Run(code, PlaygroundOutput(output));
	  	}
	}).

	// Formatting code
	factory('Fmt', ['$http', function($http) {
		return function(body, error, success) {

	 	var params = $.param({'body': body});
		var headers = {'Content-Type': 'application/x-www-form-urlencoded'};
		$http.post('/fmt', params, {headers: headers}).
	  		error(function(data, status, headers, config) {
	  			console.log(data, status)
	  		}).
	  		success(function(data) {
	  			if (data.Error) { // TODO: Add highlighting
	  				error(data.Error)
	  			} else {
	  				success(data.Body)
	  			}
	  		});
		}
	}]);
